#ifndef E_CONTROLLER_QT_TREE_H
#define E_CONTROLLER_QT_TREE_H

#include "EItem.h"
#include "qtreewidget.h"
#include <DefineLibCommon.h>
#include <DefineCommon.h>
#include <memory>

#include <EController.h>
#include <QTreeWidget>


class EControllerTreeItem : public QTreeWidgetItem
{
public:
    /*Tag value to index EItemPtr*/
    long m_tag = 0;
};




/**This class is
 * */
class EControllerQtTree;
typedef SmartPtr(EControllerQtTree) EControllerQtTreePtr;
class EControllerQtTree : public QObject, public EController
{
    Q_OBJECT
public:
    EControllerQtTree();
    virtual ~EControllerQtTree();

    void setTreeWidget(QTreeWidget* w);

    /*clear TreeWidget and loop all children and build Tree Item*/
    virtual void refresh();

    /*overwrite EItemVisitor's core function*/
    virtual bool handleEItem(EItemPtr item, int depth, EItemPtr parentItem);


    /*overwrite EController's function, to return a selected EItemPtr*/
    virtual EItemPtr getSelectedItem();

    //set selected item by outer
    void setSelectedItem(EItemPtr item);

    void addItem(long tagTypeItem);
    
    virtual void deleteItem(EItemPtr item);
    virtual void deleteItem(long tag);
    virtual void copyItem(long tag);

    virtual void selectItem(EItemPtr item);

    EItemPtr getEItemByTreeItemPtr(QTreeWidgetItem* treeItem);

    QTreeWidgetItem* getTreeItemByEItemPtr(const EItemPtr& itemPtr);
    
    /*handle command*/
    virtual void handleCommand(const ECommandPtr& command);
public slots:
    void slotCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous);

protected:
    // EControllerTreeItem* newTreeItemByEItemPtr(const EItemPtr& itemPtr);
    void synTreeItemAndEItem(const EItemPtr& itemPtr, EControllerTreeItem* treeItem);
    int checkItemTypeCount(const EString& className);

    QTreeWidget* m_treeWidget;
    QMap<long, EControllerTreeItem*> m_mapTag2TreeItem;
    QMap<long, EItemPtr> m_mapTag2EItem;
    EItemPtr m_selectedEItem;

};

#endif // E_CONTROLLER_QT_TREE_H
